package com.kevin.ymqquan.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.kevin.ymqquan.R;
import com.kevin.ymqquan.application.App;

public class MaCourseDetailActivity extends Activity {

	private String isJoin;
	private String courseId;
	private String isOver;
	private String position;

	private App application;

	private Dialog dialog;

	private TextView tv_course_detail_coursename;
	private TextView tv_course_detail_coach_right;
	private TextView tv_course_detail_type_right;
	// private TextView tv_course_detail_limit_right;
	private TextView tv_course_detail_price_right;
	private TextView tv_course_detail_time_right;
	private Button btn_course_detail_in;
	private Button btn_course_detail_out;
	private Button btn_course_detail_ti;
	private ImageView iv_course_detail_back;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_macourse_detail);

		Intent i = getIntent();

		courseId = i.getStringExtra("courseId");
		String courseName = i.getStringExtra("courseName");
		String coachName = i.getStringExtra("coachName");
		String type = i.getStringExtra("type");
		String time = i.getStringExtra("time");
		String price = i.getStringExtra("price");
		String position = i.getStringExtra("position");

		init();// 初始化
		Log.i("kevinlog", isJoin + "-" + isJoin + "-" + isOver + "-" + courseId
				+ "-" + courseName + "-" + coachName + "-" + type + "-" + time
				+ "-" + "-" + price + "-" + position);

		tv_course_detail_coursename.setText(courseName);
		tv_course_detail_coach_right.setText(coachName);
		tv_course_detail_type_right.setText(type);
		// tv_course_detail_limit_right.setText(consume);
		tv_course_detail_price_right.setText(price);
		tv_course_detail_time_right.setText(time);
		addListen();
	}

	private void init() {
		// TODO Auto-generated method stub
		application = (App) getApplication();
		tv_course_detail_coursename = (TextView) findViewById(R.id.tv_course_detail_coursename);
		tv_course_detail_coach_right = (TextView) findViewById(R.id.tv_course_detail_coach_right);
		tv_course_detail_type_right = (TextView) findViewById(R.id.tv_course_detail_type_right);
//		tv_course_detail_limit_right = (TextView) findViewById(R.id.tv_course_detail_limit_right);
		tv_course_detail_price_right = (TextView) findViewById(R.id.tv_course_detail_price_right);
		tv_course_detail_time_right = (TextView) findViewById(R.id.tv_course_detail_time_right);
		btn_course_detail_in = (Button) findViewById(R.id.btn_course_detail_in);
		btn_course_detail_out = (Button) findViewById(R.id.btn_course_detail_out);
		btn_course_detail_ti = (Button) findViewById(R.id.btn_course_detail_ti);
		iv_course_detail_back = (ImageView) findViewById(R.id.iv_course_detail_back);

		iv_course_detail_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				backIntent();
			}
		});

	}

	private void addListen() {
		
		btn_course_detail_in.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent i = new Intent(MaCourseDetailActivity.this,
						CourseUserActivity.class);
				i.putExtra("courseId", courseId);
//				startActivityForResult(i, 0);
				 startActivity(i);
				// CourseDetailActivity.this.finish();
			}
		});
		
		btn_course_detail_out.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent i = new Intent(MaCourseDetailActivity.this,
						ConCourseDActivity.class);
				i.putExtra("courseId", courseId);
				i.putExtra("ti", "0");
//				startActivityForResult(i, 0);
				startActivity(i);
				// CourseDetailActivity.this.finish();
			}
		});
		
		btn_course_detail_ti.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent i = new Intent(MaCourseDetailActivity.this,
						ConCourseDActivity.class);
				i.putExtra("courseId", courseId);
				i.putExtra("ti", "1");
//				startActivityForResult(i, 0);
				startActivity(i);
				// CourseDetailActivity.this.finish();
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		// super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == RESULT_OK) {
			isJoin = data.getStringExtra("isJoin");
			addListen();
			// Log.i("kevinlog", isJoin);
			// if ("0".equals(isJoin)) {
			// btn_course_detail_apply.setText("立即报名");
			// } else {
			// btn_course_detail_apply.setText("已报名");
			// btn_course_detail_apply.setEnabled(false);
			// btn_course_detail_apply.setBackgroundColor(Color.GRAY);
			// }
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			backIntent();
		}
		return false;
	}

	private void backIntent() {
		Intent intent = new Intent(MaCourseDetailActivity.this,
				MyCourseActivity.class);
//		setResult(Integer.parseInt(isJoin), intent);
		MaCourseDetailActivity.this.finish();
	}

}
